<?php
declare (strict_types=1);

namespace app\controller;

use app\facade\Param;
use app\handler\Excel;
use app\helpers\api\ApiResponse;
use app\model\Userplan;
use think\Request;

class UserplanController
{
    use ApiResponse;

    /**
     * 显示资源列表
     *
     * @return \think\Response
     */
    public function index(Request $request)
    {
        $query = Userplan::order('id', 'desc');
        $query = Param::userplanParam($request->param(), $query);
        return $this->success($query->paginate($request->param('page_size', 15)));
    }

    public function show(Userplan $userplan)
    {
        return $this->success($userplan);
    }

    /**
     * @param Request $request
     * @return mixed
     */
    public function store(Request $request)
    {
        Userplan::create($request->param());
        return $this->created();
    }

    /**
     * 保存新建的资源
     *
     * @param \think\Request $request
     * @return \think\Response
     */
    public function save(Request $request, Userplan $userplan)
    {
        $userplan->update($request->param());
        return $this->update();
    }

    /**
     * 删除
     * @param Userplan $userplan
     * @return mixed
     */
    public function delete(Userplan $userplan)
    {
        $userplan->delete();
        return $this->destroy();
    }

    public function exportTemplate()
    {
        $datas = [
            [
                '计划ID',
                '考生姓名',
                '考生身份证号',
                '考生手机号',
                '岗位名称',
                '单位',
                '考试开始时间',
                '考试结束时间',
                '培训开始时间',
                '培训结束时间',
                '准考证号',
                '座位号',
                '通知模板ID'
            ]
        ];
        $excel = new Excel();
        $excel->exportExcel($datas, '计划模板');
    }

    public function exportData(Request $request)
    {
        $key   = [
            '计划ID',
            '考生姓名',
            '考生身份证号',
            '考生手机号',
            '岗位名称',
            '单位',
            '考试开始时间',
            '考试结束时间',
            '培训开始时间',
            '培训结束时间',
            '准考证号',
            '座位号',
            '通知模板ID'
        ];
        $query = Userplan::field('planId,studentName,IDCard,phone,post,companyName,testStartTime,testEndTime,planStartTime,planEndTime,admissionTicket,seatNumber,templateId')->order('id', 'desc');
        $query = Param::userplanParam($request->param(), $query);
        $datas = $query->select()->toArray();
        array_unshift($datas, $key);
        $excel = new Excel();
        $excel->exportExcel($datas, '计划数据');
    }

    public function import(Request $request)
    {
        $file = request()->file('exl');
        // 上传到本地服务器
        $savename = \think\facade\Filesystem::disk('public')->putFile('exl', $file);
        $exl      = new Excel();
        $data     = $exl->importExecl(app()->getRootPath() . 'init/storage/' . $savename);
        unset($data[1]);
        foreach ($data as $k => $value) {
            $exist = Userplan::where('planId', $value['A'])->where('IDCard', $value['C'])->findOrEmpty();
            if (!$exist->isEmpty()) {
                continue;
            }
            Userplan::create([
                'planId'          => $value['A'],
                'studentName'     => $value['B'],
                'IDCard'          => $value['C'],
                'phone'           => $value['D'],
                'post'            => $value['E'],
                'companyName'     => $value['F'],
                'testStartTime'   => $value['G']?gmdate("Y/m/d h:i:s",\PhpOffice\PhpSpreadsheet\Shared\Date::excelToTimestamp($value['G'])):'',
                'testEndTime'     => $value['H']?gmdate("Y/m/d h:i:s",\PhpOffice\PhpSpreadsheet\Shared\Date::excelToTimestamp($value['H'])):'',
                'planStartTime'   => $value['I']?gmdate("Y/m/d h:i:s",\PhpOffice\PhpSpreadsheet\Shared\Date::excelToTimestamp($value['I'])):'',
                'planEndTime'     => $value['J']?gmdate("Y/m/d h:i:s",\PhpOffice\PhpSpreadsheet\Shared\Date::excelToTimestamp($value['J'])):'',
                'admissionTicket' => $value['K'],
                'seatNumber'      => $value['L'],
                'templateId'      => $value['M']
            ]);
        }
        unlink(app()->getRootPath() . 'init/storage/' . $savename);
        return $this->created();
    }
}
